package com.yss.reportworld.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yss.common.core.constant.RequestDataHelper;
import com.yss.common.core.exception.BusinessException;
import com.yss.common.core.exception.base.BaseException;
import com.yss.common.core.utils.easyexecl.EasyExeclUtil;
import com.yss.reportworld.service.ISqlScriptService;
import com.yss.reportworld.util.TableInfoUtil;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yss.common.logger.annotation.Log;
import com.yss.common.logger.enums.BusinessType;
import com.yss.common.security.annotation.RequiresPermissions;
import com.yss.reportworld.domain.Report;
import com.yss.reportworld.service.IReportService;
import com.yss.common.core.web.controller.BaseController;
import com.yss.common.core.web.domain.AjaxResult;
import org.springframework.web.multipart.MultipartFile;

/**
 * 报表信息Controller
 *
 * @author yss
 * @date 2023-03-24
 */
@RestController
@RequestMapping("/report")
public class ReportController extends BaseController {
    @Autowired
    private IReportService reportService;
    @Autowired
    private ISqlScriptService sqlScriptService;

    /**
     * 查询报表信息列表
     */
    @RequiresPermissions("reportworld:report:list")
    @GetMapping("/list")
    public AjaxResult list(Report report) {
        List<Report> list = reportService.selectReportList(report);
        return success(list);
    }


    /**
     * 初始化 加载
     */
    @RequiresPermissions("reportworld:report:list")
    @Log(title = "报表信息", businessType = BusinessType.OTHER)
    @PostMapping("/init")
    public AjaxResult init(@RequestBody Report report) {
        try {
            TableInfoUtil.handleTableVersion(report.getHisVersion()); // 替换表名
            AjaxResult ajax = AjaxResult.success();
            ajax.put("hisVersions", sqlScriptService.getReductionVersion(false));
            return ajax;
        } catch (Exception ex) {
            throw new BusinessException("初始化加载数据出错，请稍后重试！" + ex.getMessage());
        } finally {
            RequestDataHelper.removeThreadLocal();
        }
    }

    /**
     * 导出报表信息列表
     */
    @RequiresPermissions("reportworld:report:export")
    @Log(title = "报表信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Report report) {
        List<Report> list = reportService.selectReportList(report);
        EasyExeclUtil.export(response, list, Report.class);
    }

    /**
     * 获取报表信息详细信息
     */
    @RequiresPermissions("reportworld:report:query")
    @PostMapping(value = "/getReport")
    public AjaxResult getReport(@RequestBody Report report) {
        return success(reportService.selectReportById(report));
    }

    /**
     * 新增报表信息
     */
    @RequiresPermissions("reportworld:report:add")
    @Log(title = "报表信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Report report) {
        return toAjax(reportService.addReport(report));
    }

    /**
     * 修改报表信息
     */
    @RequiresPermissions("reportworld:report:edit")
    @Log(title = "报表信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Report report) {
        return toAjax(reportService.editReport(report));
    }

    /**
     * 删除报表信息
     */
    @RequiresPermissions("reportworld:report:remove")
    @Log(title = "报表信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody Report report) {
        reportService.delReport(report);
        return success();
    }

}
